﻿using NewfarTormentTool.data;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;

namespace NewfarTormentTool.core
{
    /// <summary>
    /// 用户记录
    /// </summary>
    public class UserRecord
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string user { get; set; }
        /// <summary>
        /// 可用角色IDs
        /// </summary>
        public List<int> actives { get; set; }

        public UserRecord()
        {
            actives = new List<int>();
        }

        /// <summary>
        /// 从文件中加载
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="user">用户</param>
        public static UserRecord LoadFromFile(string fileName, string user)
        {
            FileStream fs = null;
            try
            {
                using (fs = File.OpenRead(fileName))
                {
                    IWorkbook workbook;
                    if (fileName.EndsWith(".xlsx"))
                    {
                        workbook = new XSSFWorkbook(fs);
                    }
                    else if (fileName.EndsWith(".xls"))
                    {
                        workbook = new HSSFWorkbook(fs);
                    }
                    else
                    {
                        return null;
                    }
                    if (workbook != null)
                    {
                        UserRecord record = ParseFromSheet(workbook.GetSheetAt(0));
                        if (record == null)
                        {
                            return null;
                        }
                        record.user = user;
                        return record;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                // 最终关闭文件流
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }

        /// <summary>
        /// 从Sheet解析记录
        /// </summary>
        private static UserRecord ParseFromSheet(ISheet sheet)
        {
            if (sheet == null)
            {
                return null;
            }
            try
            {
                IRow headerRow = sheet.GetRow(sheet.FirstRowNum);
                // ID所在列
                int index_id = -1;
                // 状态所在列
                int index_state = -1;
                for (int i = 0; i < headerRow.LastCellNum; i++)
                {
                    switch (headerRow.GetCell(i).StringCellValue)
                    {
                        case "ID":
                            {
                                index_id = i;
                            }
                            break;
                        case "State":
                            {
                                index_state = i;
                            }
                            break;
                    }
                }
                if (index_id < 0 || index_state < 0)
                {
                    return null;
                }
                UserRecord record = new UserRecord();
                for (int i = sheet.FirstRowNum + 2; i <= sheet.LastRowNum; i++)
                {
                    IRow dataRow = sheet.GetRow(i);
                    if (dataRow == null)
                    {
                        continue;
                    }
                    try
                    {
                        int id = int.Parse(Tools.get_cell_str(dataRow.GetCell(index_id)));
                        bool active = Tools.get_cell_str(dataRow.GetCell(index_state)).Contains("1");
                        if (active && RoleDataStorage.Instance.Configs.ContainsKey(id))
                        {
                            record.actives.Add(id);
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
                return record;
            }
            catch
            {
                return null;
            }
        }
    }
}
